//
//  ModalViewController.m
//  Popping
//
//  Created by André Schneider on 16.05.14.
//  Copyright (c) 2014 André Schneider. All rights reserved.
//

#import "ThirdViewController.h"
#import "Insurances.h"
#define kCellIdentifier_Cell  @"mycell"
@interface ThirdViewController()

- (void)dismiss:(id)sender;
@property (strong, nonatomic) UITableView *myTableView;
@end

@implementation ThirdViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.layer.cornerRadius = 8.f;
    self.view.backgroundColor = kColorTheme;
}

- (void)loadView{
    [super loadView];

    CGRect frame = CGRectMake(0, 0, kScreen_Width, kScreen_Height);
    self.view = [[UIView alloc] initWithFrame:frame];
    self.title = @"车险类型";
    _myTableView = ({
        CGSize size = self.view.frame.size;
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, size.width - 80.f,size.height -288.f) style:UITableViewStylePlain];
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellIdentifier_Cell];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.view addSubview:tableView];
        tableView.layer.cornerRadius = 8.f;
        tableView;
    });
    _myTableView.tableFooterView = [self tableFooterView];
    
    
    

}
#pragma mark Table M
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[_ins getThirdlist] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_Cell];
    
    NSString *money = [[_ins getThirdlist] objectAtIndex:indexPath.row];
    NSString *text = [NSString stringWithFormat:@"第三者责任险(%@万)",money];
    cell.textLabel.text = text;
    if ([money isEqualToString:_ins.third]) {
        cell.imageView.image = [UIImage imageNamed:@"选中"];
    }
    else{
        cell.imageView.image = [UIImage imageNamed:@"未选"];
    }
    return cell;
}

#pragma mark - Private Instance methods
- (UIView*)tableFooterView{
    
    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _myTableView.frame.size.width, 50)];
    
    UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeSystem];
    dismissButton.frame = CGRectMake(0, 10,  _myTableView.frame.size.width, 20);
    dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
    dismissButton.tintColor = [UIColor blackColor];
    dismissButton.titleLabel.font = [UIFont fontWithName:@"Avenir" size:20];
    [dismissButton setTitle:@"确认" forState:UIControlStateNormal];
    [dismissButton addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];

    [footerV addSubview:dismissButton];
    return footerV;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    _ins.third = [[_ins getThirdlist] objectAtIndex:indexPath.row];
    [_ins saveInfo];
    [tableView reloadData];


}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
  //  [tableView reloadData];

}

- (void)dismiss:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

@end
